package de.ohmhochschule.bme;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class UrlRegExpActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		try {
			URL url = new URL("http://androidproxy.in/what-my-android-ip.html");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					url.openStream()));
			StringBuffer webPage = new StringBuffer();
			String line;
			while (!(null == ( line = br.readLine() ))) {
				webPage.append(line);
			}
			br.close();
			Log.i(getLocalClassName(), webPage.toString());
			
			String sIp = "No IP found.";
			String regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";
			Pattern pattern = Pattern.compile(regexp);
			Matcher match = pattern.matcher(webPage.toString());
			if ( match.find() ) {
				sIp = "My IP is: " + match.group(1);
			}

	        TextView textView1 = (TextView) findViewById(R.id.textView1);
	        textView1.setText(sIp);

	        TextView textView2 = (TextView) findViewById(R.id.textView2);
	        textView2.setText(webPage.toString());

		} catch (Exception e) {
			Log.e(getLocalClassName(), e.getMessage());
		}

	}
}